package com.deng.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author 邓小熙
 * @Date 2022/5/8 17:26
 * @Version 1.0
 */
@Controller
public class MainController {

    @RequestMapping("/main/index")
    public String mainIndex() {
        return "/main/index";
    }

    @RequestMapping("/main/content")
    public String mainContent() {
        return "/main/content";
    }

    @RequestMapping(value = "/main/columnItem/{columnId}")
    public ModelAndView columnItem(@PathVariable("columnId") String columnId) {
        ModelAndView modelAndView = new ModelAndView("/main/columnItem");
        modelAndView.addObject("columnId", columnId);
        return modelAndView;
    }

    /**
     * 个人简介
     *
     * @param userName
     * @return
     */
    @RequestMapping("/main/personal/{userName}")
    public ModelAndView personal(@PathVariable("userName") String userName) {
        ModelAndView modelAndView = new ModelAndView("/main/personal");
        modelAndView.addObject("userName", userName);
        return modelAndView;
    }

    /**
     * 他人的个人简介
     *
     * @param friendName
     * @return
     */
    @RequestMapping("/InnerLayui/friendPersonal/{friendName}")
    public ModelAndView friendPersonal(@PathVariable("friendName") String friendName) {
        ModelAndView modelAndView = new ModelAndView("/InnerLayui/friendPersonal");
        modelAndView.addObject("friendName", friendName);
        return modelAndView;
    }

    /**
     * 文章库
     *
     * @return
     */
    @RequestMapping("/main/pageList")
    public String pageList() {
        return "/main/pageList";
    }

    /**
     * 博客专栏
     *
     * @return
     */
    @RequestMapping("/main/columnList")
    public String columnList() {
        return "/main/columnList";
    }


    @RequestMapping(value = "/upload/img", method = {RequestMethod.POST})
    @ResponseBody
    public Object upload(@RequestParam(value="file",required=false) MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String,Object> map=new HashMap<>();
        String name = request.getParameter("name");//获取data中数据
        if (file!=null){
            //获取文件名
            String fileName = file.getOriginalFilename();
            map.put("code",0);
        }else {
            map.put("code",1);
        }

        return map;
    }
    //图片上传
    @ResponseBody
    @RequestMapping("/uploadFile")
    public JSON uploadFile(MultipartFile file, HttpServletRequest request) {
        JSONObject json = new JSONObject();
        //路径
        String filePath = System.getProperty("user.dir") + "/src/main/resources/static/main/images/";//上传到这个文件夹
        File file1 = new File(filePath);
        //如果没有的话创建一个
        if (!file1.exists()) {
            file1.mkdirs();
        }

        //路径+文件名
        //文件名：file.getOriginalFilename()
        String finalFilePath = filePath + file.getOriginalFilename().trim();
        File desFile = new File(finalFilePath);
        if (desFile.exists()) {
            desFile.delete();
        }
        try {
            file.transferTo(desFile);
            json.put("code", 0);
            //将文件名放在msg中，前台提交表单时需要用到
//            json.put("msg", file.getOriginalFilename().trim());
            request.getSession().setAttribute("msg","/main/images/" + file.getOriginalFilename().trim());
//            request.setAttribute("msg",filePath+file.getOriginalFilename().trim());
//            JSONObject json2 = new JSONObject();
//            json2.put("src", finalFilePath);
//            json2.put("title", "");
//            json.put("Data", json2);

        } catch (Exception e) {
            System.out.println(e.getMessage());
            json.put("code", 1);
        }
        System.out.println(filePath+file.getOriginalFilename().trim());
        return json;
    }
}

